package crivia.ecp.carry.em;

import crivia.db.common.SQL;
import crivia.db.i.DataSet;
import crivia.ecp.carry.html.X;
import crivia.ecp.carry.html.div.Div;
import crivia.ecp.carry.html.form.Input;
import crivia.ecp.common.ECR;
import crivia.ecp.p.implemented.E8BrowserComboBoxDataFrom;

public class Browser8 extends Browser {

	@Deprecated
	public Browser8(){
		super(null, null, null, null);
	}

	public Browser8(String url, String key, String value, String requirdString) {
		super(url, key, value, requirdString);
	}

	@Override
	public String toString() {
		
		X script = X.JavaSctipt("" 
		+ "\njQuery(document).ready(function() {"
		+ "\n		__browserNamespace__.e8formatInitData({" 
		+ "\n			nameAndId : '" + getScriptKey() + "'"
		+ "\n			,name : '" + getScriptKey() + "'"
		+ "\n			,isMustInput : 1"
		+ "\n			,hasInput : false" 
		+ "\n			,browserBox : ''" 
		+ "\n		});" 
		+ (ECR.hasCondition(completeURL) ? completeScript(completeURL)
			: relationReady() ? completeScript(E8BrowserComboBoxDataFrom
					.EU(E8BrowserComboBoxDataFrom.class)
				+"?f="+relationID+","+relationName+"&t="+relationTable)
			: "")
		+ "\n});");

		EditerController controller = new EditerController(
			getScriptKey(), EditerController.Type.Browser
			, ECR.ns(getDefaultValue()), ""
			, isRequird(), ECR.ns(getRequirdString()), getRequirdStringLevel());
		
		return ""+box()+script+X.JavaSctipt("_E.editer.create("+controller
				.setRequirdStringScript(requirdStringScript)+");");
	}

	protected Object valueShow(){
		if (ECR.hasCondition(fixText)){
			return X.Browser8Value(fixText);
		}
		if (! ECR.hasCondition(value)){
			return "";
		}
		if (! relationReady()){
			return "";
		}
//		String text = crivia.db.common.SQL.relation(ECR.ns(value)
//				, relationTable, relationName, relationID, relationCMD);
		DataSet data = SQL.select(SQL.sql.array(
				"select " + relationID + " v"
				+ "," + relationName + " t"
				+ " from " + relationTable + (
					(true
						&& relationTable.trim().startsWith("(")
						&& relationTable.trim().endsWith(")"))
					? " t" : ""
				) + " where " + relationID + " in (?)"
				, ECR.ns(ECR.ns(value)).replaceAll(",", "','")) , relationCMD);
		if (data.size() < 1){
			return "";
		}
//		if (! ECR.hasCondition(text)){
//			return "";
//		}
		StringBuilder tStr = new StringBuilder();
//		for (String s : text.split(",")){
		while (data.next()){
			tStr.append(X.Browser8Value(data.get("t"),data.get("v")));
		}
		return tStr;
	}
	
	private String completeScript(String completeUrl){
		return ""
				+ "\n		window.setTimeout(function() {"
				+ "\n			__browserNamespace__.hoverShowNameSpan('.e8_showNameClass');"
				+ "\n			__browserNamespace__.e8autocomplete({"
				+ "\n			nameAndId : '"+getScriptKey()+"',"
				+ "\n				inputNameAndId : '"+getScriptKey()+"__',"
				+ "\n				isMustInput : 1,"
				+ "\n				hasAdd : false,"
				+ "\n				completeUrl : '"+completeUrl.replaceAll("'", "\\\\'")+"',"
				+ "\n				isSingle : "+(!isMulti())+","
				+ "\n				extraParams : {"
				+ "\n					\"_exclude\" : \"getNameAndIdVal\""
				+ "\n				},"
				+ "\n				row_height : 22,"
				+ "\n				linkUrl : '#',"
				+ "\n				needHidden : true,"
				+ "\n				sb : {},"
				+ "\n				_callback : '',"
				+ "\n				type : '',"
				+ "\n				browserBox : ''"
				+ "\n			});"
				+ "\n		} , 500);";
	}
	
	private Div box(){
		Input hidden = new Input(key, value, Input.Type_Hidden, null);
		hidden.setScriptKey(getScriptKey());
		hidden.isReg = false;
		hidden.addAttr("isMustInput", "1")
			.addAttr("isSingle", ""+(!isMulti()))
			.addAttr("viewtype", "0");
		
		Input cbx = new Input(getScriptKey()+"__",null);
		cbx.isReg = false;
		cbx.setCssClass("e8_browserInputMore ac_input")
			.addAttr("onkeydown", "__browserNamespace__.delByBS(event,'"+getScriptKey()+"__',1,true,{});")
			.addAttr("onblur", "__browserNamespace__.setAutocompleteOff(this);")
			.addStyle("width", "17px");
		
		return (Div) Div.createByClass("e8_os").setInner(""
			+Div.createByClass("e8_innerShow"
					+" e8_innerShow_button e8_innerShow_button_right30").setInner(""
				+new X("span"
					,new X("span"
						,new X("button", "")
						.setCssClass("e8_browflow")
						.addAttr("id", getScriptKey() + "_browserbtn")
						.addAttr("type", "button")
						.addAttr("xurl", url)
						.addAttr("onclick","__browserNamespace__"
							+ ".showModalDialogForBrowser(event"
+ ",( jQuery(this).attr('xurl')=='"+URL_HrmMuti+"'"
	+"?jQuery(this).attr('xurl')+'?resourceids='+_E.v('"+getScriptKey()+"')"
+":jQuery(this).attr('xurl').indexOf('"+BrowserURLSystemBase
		+"/interface/MultiCommonBrowser.jsp')==0"
	+"?jQuery(this).attr('xurl')"
		+"+'&beanids='+_E.v('"+getScriptKey()+"')"
		+"+'&selectedids='+_E.v('"+getScriptKey()+"')"
+":jQuery(this).attr('xurl') )"
							+ ",'#','"+getScriptKey()+"',true,1,''"
							+ ",{name:'"+getScriptKey()+"',hasInput:false,zDialog:true"
							+ ",needHidden:true,dialogTitle:'',arguments:''});"))
					.setCssClass("e8_browserSpan"))
				.setCssClass("e8_spanFloat"))
			+Div.createByClass("e8_innerShow e8_innerShowMust").setInner(""
				+new X("span","")
				.setCssClass("e8_spanFloat")
				.addAttr("id", getScriptKey() + "spanimg")
				.addAttr("name", getScriptKey() + "spanimg"))
			+Div.createByClass("e8_outScroll").setInner(""
				+Div.createByClass("e8_innerShow e8_innerShowContent").setInner(""
					+new Div().setInner(""
						+hidden
						+new X("span", valueShow())
						.addStyle("float", "none")
						.addAttr("id", getScriptKey() + "span")
						.addAttr("name", getScriptKey() + "span")
						+(relationReady()?cbx:""))
					.addStyle("margin-left", "31px")
					.addAttr("id", "inner" + getScriptKey() + "div")
					.addAttr("hasBrowser", "true")
					.addAttr("hasAdd", "false"))
				.addStyle("max-height", "2200px")
				.addStyle("margin-right", "30px")
				.addStyle("overflow-y", "hidden")
				.addAttr("id", "innerContent" + getScriptKey() + "div"))
			.addStyle("width", "100%")
			.addStyle("margin-right", "-30px")
			.addAttr("id", "out" + getScriptKey() + "div")).addStyle("width", width);
		
	}

}
